/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [StudentLogicImpl.java]
 * Creation Date        [12-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_04_other.examApp.dao.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;

import pkg_04_other.examApp.dao.ExaminationAdapter;
import pkg_04_other.examApp.entity.Student;
import pkg_04_other.examApp.util.ExaminationUtil;

/**
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class StudentLogicImpl extends ExaminationAdapter {

	@Override
	public boolean login(Student student) {
		boolean flag = false;

		try {
			List<String> list = ExaminationUtil
					.readFile("src_03_best_practices/pkg_04_other/examApp/configuration"
							+ File.separator + "student.cfg");

			for (String studentInfo : list) {
				String[] info = studentInfo.split(":");
				int id = Integer.parseInt(info[0]);
				Student stu = new Student(id, info[1], info[2]);
				if (student.equals(stu)) {
					if (student.getPassword().equals(stu.getPassword())) {
						super.setId(id);
						super.setName(stu.getName());
						flag = true;
						break;
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return flag;
	}
}
